<template>
  <div class="await">
    <div class="search">
      <BaseNewForm
        ref="searchForm"
        :spanNumber="8"
        :formRow="SearchformRow"
        :ruleForm="searchForm"
        :isFunBtn="false"
        @onSelect="searchSelectChangeHandle"
        :newFlag="true"
        :formRule="false"
      ></BaseNewForm>
      <div class="btn">
        <el-button type="primary" @click="searchHandle">搜索</el-button>
        <el-button @click="resizeSearchHandle">重置</el-button>
      </div>
    </div>
    <div class="maintable">
      <div class="left">
        <BaseTable
          ref="mainTable"
          :tableData="tableData"
          :tableColumn="tableColumn"
          :tabLoading="mainTabLoading"
          :tableHeight="600"
          :funData="mainFunData"
          @onFunc="tableButtonHandle"
          :border="false"
        >
          <template v-slot:vou="{ row }">
            <div>{{ row.row.vou ? vou_dist[row.row.vou * 1] : "" }}</div>
          </template>
          <template v-slot:certificate_status="{ row }">
            <div>
              {{
                row.row.certificate_status
                  ? certificate_status_dist[row.row.certificate_status * 1]
                  : ""
              }}
            </div>
          </template>
        </BaseTable>
        <div class="footer">
          <basePage
            :pageModel="pageModel"
            @update:pageModel="currentChangeHandle"
          ></basePage>
        </div>
      </div>
    </div>
    <baseDialog
      class="dialog"
      :loading="dialogCommitLoading"
      v-model="dialogSwitch"
      :fullscreen="true"
      @close="dialogCloseHandle"
      @confirm="dialogconfirmHandle"
      :footerShow="true"
      :lookFlag="lookFlag"
      :title="dontmake ? '不制单备注' : '凭证制单'"
    >
      <template v-if="dontmake">
        <div>
          <el-input
            type="textarea"
            :rows="4"
            placeholder="请输入内容"
            v-model="dontmakeRemark"
          >
          </el-input>
        </div>
      </template>
      <template v-else>
        <div class="receipts">
          <div class="title">{{ receiptsTitle }}</div>
          <div class="date">
            <span class="demonstration">制单时间:</span>
            <div class="datepick">
              <el-date-picker
                :disabled="true"
                v-model="receiptsDate"
                type="date"
                placeholder="选择日期"
                value-format="yyyy-MM-dd"
                size="mini"
              >
              </el-date-picker>
            </div>
          </div>
          <div class="main">
            <div class="top">
              <div class="left">
                <div class="select">
                  <el-select
                    v-model="receiptsSelect"
                    placeholder=""
                    :disabled="true"
                  >
                    <el-option
                      v-for="item in receiptsOptions"
                      :key="item.id"
                      :label="item.label"
                      :value="item.id"
                    >
                    </el-option>
                  </el-select>
                </div>
                <div class="input">
                  <el-input
                    v-model="receiptsnumber"
                    :disabled="true"
                    placeholder=""
                  ></el-input>
                </div>
              </div>
              <div class="right">
                <el-button type="primary" @click="addTableRow" :disabled="true"
                  >新增行</el-button
                >
                <el-button :disabled="true">删除行</el-button>
              </div>
            </div>
            <div class="table">
              <BaseTable
                ref="receiptsTable"
                :showIndex="true"
                :highlightCurrent="true"
                @onRowClick="tableClick"
                :tableData="receiptsTableData"
                :tableColumn="receiptsTableColumn"
                :tabLoading="receiptsabLoading"
                @sumsChange="sumsChangeHandle"
                :showsummary="true"
                :tableHeight="400"
                :receipSum="receipSum"
                :border="true"
                :SummariesIndex="[3, 4]"
              >
                <!-- 摘要 -->
                <template v-slot:abstract_msg="{ row: scope }">
                  <div v-if="!scope.row[scope.column.property + 'show']">
                    {{ scope.row[scope.column.property] }}
                  </div>
                  <div v-else>
                    <el-input
                      v-model="scope.row[scope.column.property]"
                      :ref="scope.column.property + scope.$index"
                      @blur="scope.row[scope.column.property + 'show'] = false"
                    ></el-input>
                  </div>
                </template>
                <!-- 科目 -->
                <template v-slot:subject="{ row: scope }">
                  <!-- <div v-if="!scope.row[scope.column.property + 'show']">
                    {{ scope.row[scope.column.property] }}
                  </div>
                  <div v-else> -->
                  <baseNewSelect
                    :lookflag="!scope.row[scope.column.property + 'show']"
                    :ref="scope.column.property + scope.$index"
                    v-model="scope.row[scope.column.property]"
                    :itemObj="scope.row"
                    @showValue="showValueHandle"
                    @blur="scope.row[scope.column.property + 'show'] = false"
                    :idKey="scope.column.property"
                  ></baseNewSelect>
                  <!-- </div> -->
                </template>
                <!-- 借方金额 -->
                <template v-slot:debit_amount="{ row: scope }">
                  <div v-if="!scope.row[scope.column.property + 'show']">
                    {{ scope.row[scope.column.property] }}
                  </div>
                  <div v-else>
                    <el-input-number
                      v-model="scope.row[scope.column.property]"
                      :ref="scope.column.property + scope.$index"
                      controls-position="right"
                      @blur="
                        jineoneBlur(
                          scope.row,
                          scope.row[scope.column.property],
                          scope.row[scope.column.property + 'show']
                        )
                      "
                      @keyup.enter.native="
                        jineoneBlur(
                          scope.row,
                          scope.row[scope.column.property],
                          scope.row[scope.column.property + 'show']
                        )
                      "
                      :min="1"
                      :max="99999"
                    ></el-input-number>
                  </div>
                </template>
                <!-- 贷方金额 -->
                <template v-slot:credit_amount="{ row: scope }">
                  <div v-if="!scope.row[scope.column.property + 'show']">
                    {{ scope.row[scope.column.property] }}
                  </div>
                  <div v-else>
                    <el-input-number
                      v-model="scope.row[scope.column.property]"
                      :ref="scope.column.property + scope.$index"
                      controls-position="right"
                      @blur="
                        credit_amountBlur(
                          scope.row,
                          scope.row[scope.column.property],
                          scope.row[scope.column.property + 'show']
                        )
                      "
                      @keyup.enter.native="
                        credit_amountBlur(
                          scope.row,
                          scope.row[scope.column.property],
                          scope.row[scope.column.property + 'show']
                        )
                      "
                      :min="1"
                      :max="99999"
                    ></el-input-number>
                  </div>
                </template>
              </BaseTable>
            </div>
            <div class="footer">
              <div class="left">
                <div class="top">
                  <div class="left">
                    分 录：{{
                      receiptsclickRow.index + 1
                        ? receiptsclickRow.index + 1
                        : ""
                    }}
                  </div>
                  <div class="right">
                    <el-radio
                      :disabled="true"
                      v-model="receiptsclickRow.direction"
                      label="1"
                      >借</el-radio
                    >
                    <el-radio
                      :disabled="true"
                      v-model="receiptsclickRow.direction"
                      label="2"
                      >贷</el-radio
                    >
                  </div>
                </div>
                <div class="bottom">
                  科 目：{{ receiptsclickRow.subjectShowValue }}
                </div>
              </div>
              <div class="right">
                <div v-if="receiptsclickRow.project_flag" class="chunk">
                  <div class="smalltitle">项目：</div>
                  <div class="value">
                    <BaseRightLeftSelect
                      v-model="receiptsclickRow.project"
                      :lookflag="true"
                      :journalingType="'project'"
                    ></BaseRightLeftSelect>
                  </div>
                </div>
                <div v-if="receiptsclickRow.suppliter_flag" class="chunk">
                  <div class="smalltitle">供应商：</div>
                  <div class="value">
                    <BaseRightLeftSelect
                      v-model="receiptsclickRow.suppliter"
                      :lookflag="true"
                      :journalingType="'supplier'"
                    ></BaseRightLeftSelect>
                  </div>
                </div>
                <div v-if="receiptsclickRow.client_flag" class="chunk">
                  <div class="smalltitle">客户：</div>
                  <div class="value">
                    <BaseRightLeftSelect
                      v-model="receiptsclickRow.client"
                      :lookflag="true"
                      :journalingType="'client'"
                    ></BaseRightLeftSelect>
                  </div>
                </div>
                <div v-if="receiptsclickRow.salesman_flag" class="chunk">
                  <div class="smalltitle">业务员：</div>
                  <div class="value">
                    <BaseRightLeftSelect
                      v-model="receiptsclickRow.salesman"
                      :lookflag="true"
                      :journalingType="'salesman'"
                    ></BaseRightLeftSelect>
                  </div>
                </div>
                <div v-if="receiptsclickRow.section_flag" class="chunk">
                  <div class="smalltitle">部门：</div>
                  <div class="value">
                    <BaseRightLeftSelect
                      v-model="receiptsclickRow.section"
                      :lookflag="true"
                      :journalingType="'section'"
                    ></BaseRightLeftSelect>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <footer>
            <div class="top">
              <div class="chunk">
                <div class="smtitle">账套名称：</div>
                <div class="value">{{ attribute.sob }}</div>
              </div>
            </div>
            <div class="bottom">
              <div class="chunk">
                <div class="smtitle">账套年：</div>
                <div class="value">{{ attribute.year }}</div>
              </div>
              <div class="chunk">
                <div class="smtitle">制单：</div>
                <div class="value">{{ attribute.documenterman }}</div>
              </div>
              <div class="chunk">
                <div class="smtitle">审核：</div>
                <div class="value">{{ attribute.auditorman }}</div>
              </div>
              <div class="chunk">
                <div class="smtitle">审核日期：</div>
                <div class="value">{{ attribute.auditor_date }}</div>
              </div>
              <div class="chunk">
                <div class="smtitle">记帐：</div>
                <div class="value">{{ attribute.bookkeeperman }}</div>
              </div>
              <div class="chunk">
                <div class="smtitle">出纳：</div>
                <div class="value">{{ attribute.tellerman }}</div>
              </div>
            </div>
          </footer>
        </div>
      </template>
    </baseDialog>
  </div>
</template>

<script>
import BaseNewForm from "./compoments/baseNewForm";
import BaseTable from "./compoments/baseTable";
import basePage from "./compoments/basePage.vue";
import baseDialog from "./compoments/baseDialog.vue";
import baseNewSelect from "./compoments/baseNewSelect.vue";
import ToString from "./utils/numChange";
import { deepClone } from "@/utils/index.js";
import BaseRightLeftSelect from "./compoments/baseLeftRightSelect.vue";
import { getUserModuleApi } from "@/api/integrationOption/integrationOption.js";
export default {
  data() {
    return {
      attribute: {},
      vou_dist: {
        1: "转",
        2: "银行付",
        3: "银收",
        4: "现收",
        5: "现付",
        6: "调整",
      },
      certificate_status_dist: {
        1: "未推送",
        2: "已推送",
        3: "出纳审核",
        4: "推送失败",
        5: "已经推送撤销",
        6: "已删除",
      },
      lookFlag: true,
      mainFunData: [
        {
          type: "view",
          text: "查看",
          color: "#5a9cf8",
        },
      ],
      // 凭证相关数据
      assistformRow: [],
      assistLoading: false,
      assistdialogSwitch: false,
      receipSum: 0,
      dictionaries: {},
      receiptsabLoading: false,
      receiptsclickRow: {},
      receiptsclickRowClone: {},
      receiptsnumber: "11",
      receiptsTableData: [
        {
          abstract_msg: "摘要1",
          subject: "1",
          direction: "1",
          abstract_msgshow: false,
          subjectshow: false,
          debit_amountshow: false,
          credit_amountshow: false,
          project: "1",
          project_flag: true,
          suppliter: "2",
          suppliter_flag: true,
          client: "3",
          client_flag: true,
          salesman: "4",
          salesman_flag: true,
          section: "5",
          section_flag: true,
        },
        {
          abstract_msg: "摘要2",
          subject: "2",
          direction: "2",
          abstract_msgshow: false,
          subjectshow: false,
          debit_amountshow: false,
          credit_amountshow: false,
        },
      ],
      receiptsTableColumn: [
        {
          id: "abstract_msg",
          title: "摘要",
          width: 300,
        },
        {
          id: "subject",
          title: "科目",
          total: 20,
          width: 600,
        },
        {
          id: "debit_amount",
          title: "借方金额",
        },
        {
          id: "credit_amount",
          title: "贷方金额",
        },
      ],
      receiptsOptions: [
        {
          id: 1,
          label: "转",
          title: "转账凭证",
        },
        {
          id: 2,
          label: "银付",
          title: "银行付款凭证",
        },
        {
          id: 3,
          label: "银收",
          title: "银行收款证",
        },
        {
          id: 4,
          label: "现收",
          title: "现金收款凭证",
        },
        {
          id: 5,
          label: "现付",
          title: "现金付款凭证",
        },
        {
          id: 6,
          label: "调整",
          title: "调整器凭证",
        },
      ],
      receiptsSelect: 2,
      receiptsDate: "",
      //   不制单相关
      dontmakeRemark: "",
      dontmake: false,
      dialogSwitch: false,
      dialogCommitLoading: false,
      pageModel: {
        pageIndex: 1,
        total: 10,
        limit: 10,
      },
      mainTabLoading: false,
      searchForm: {},
      SearchformRow: [
        {
          elCol: [
            {
              type: "newSelect",
              title: "账套查询",
              id: "sob_no",
              row: 8,
              journalingType: "sob",
            },
            {
              type: "select",
              title: "会计期间",
              id: "ap",
              row: 8,
              options: [
                {
                  id: 1,
                  label: "第1期",
                },
                {
                  id: 2,
                  label: "第2期",
                },
                {
                  id: 3,
                  label: "第3期",
                },
                {
                  id: 4,
                  label: "第4期",
                },
                {
                  id: 5,
                  label: "第5期",
                },
                {
                  id: 6,
                  label: "第6期",
                },
                {
                  id: 7,
                  label: "第7期",
                },
                {
                  id: 8,
                  label: "第8期",
                },
                {
                  id: 9,
                  label: "第9期",
                },
                {
                  id: 10,
                  label: "第10期",
                },
                {
                  id: 11,
                  label: "第11期",
                },
                {
                  id: 12,
                  label: "第12期",
                },
              ],
            },
            {
              type: "input",
              title: "协同标题",
              id: "receipt_title",
              row: 8,
            },
          ],
        },
      ],
      tableData: [],
      tableColumn: [
        {
          id: "ap",
          title: "会计期间",
          width: 100,
        },
        {
          id: "vou",
          title: "凭证字",
          width: 300,
          width: 100,
        },
        {
          id: "voucher_number",
          title: "凭证号",
          width: 100,
        },
        {
          id: "sob_no",
          title: "账套号",
          width: 100,
        },
        {
          title: "标题",
          id: "receipt_title",
        },
        {
          id: "year",
          title: "年度",
          width: 100,
        },
        {
          id: "push_date",
          title: "推送日期",
          width: 130,
        },
        {
          id: "certificate_status",
          title: "凭证状态",
          width: 100,
        },
      ],
      checkTable: [],
    };
  },
  components: {
    BaseNewForm,
    BaseTable,
    basePage,
    baseDialog,
    baseNewSelect,
    BaseRightLeftSelect,
  },
  methods: {
    // 初始化主表
    async initMainTableHandele(obj = {}) {
      const res = await getUserModuleApi(
        {
          tl: "sys_proofService",
          as: "sys_proofService",
          dj: "queryProofPage",
        },
        {
          pageNum: this.pageModel.pageIndex,
          pageSize: this.pageModel.limit,
          ...obj,
        }
      );
      if (res.status == 200) {
        this.pageModel.total = res.attribute.total;
        this.tableData = res.attribute.list;
      }
    },
    async finishHandle(id) {
      const res = await getUserModuleApi(
        {
          tl: "sys_proofService",
          as: "sys_proofService",
          dj: "getProof",
        },
        { id: id }
      );
      this.maleReceiptsLoding = false;
      if (res.status == 200) {
        this.dialogSwitch = true;
        this.attribute = res.attribute;
        this.searchSob(res.attribute.sob_no, "sob").then((val) => {
          this.attribute.sob = val;
        });
        if (res.attribute.documenter) {
          this.searchSob(res.attribute.documenter, "salesman").then((val) => {
            this.attribute.documenterman = val;
          });
        }
        if (res.attribute.auditor) {
          this.searchSob(res.attribute.auditor, "salesman").then((val) => {
            this.attribute.auditorman = val;
          });
        }

        res.attribute.bookkeeper &&
          this.searchSob(res.attribute.bookkeeper, "salesman").then((val) => {
            this.attribute.bookkeeperman = val;
          });
        res.attribute.teller &&
          this.searchSob(res.attribute.teller, "salesman").then((val) => {
            this.attribute.tellerman = val;
          });
        this.dontmake = false;
        this.dialogSwitch = true;
        this.receiptsSelect = res.attribute.vou * 1;
        this.receiptsnumber = res.attribute.voucher_number;
        this.receiptsTableData = [];
        this.receiptsDate = res.attribute.certificate_date;
        let obj = {
          abstract_msgshow: false,
          subjectshow: false,
          debit_amountshow: false,
          credit_amountshow: false,
        };
        res.attribute.proofDetails.forEach((item) => {
          this.receiptsTableData.push({ ...item, ...obj });
        });
      }
    },
    // 寻找各种数据/账套/业务员
    async searchSob(val, type) {
      const res = await getUserModuleApi(
        {
          tl: "sys_proofService",
          as: "sys_proofService",
          dj: "getJournaling",
        },
        {
          journalingType: type,
          codes: val,
        }
      );
      // this.attribute.sob = res.attribute.names;
      return res.attribute.names;
    },
    tableButtonHandle(val, item) {
      this.finishHandle(val.id);
    },
    dialogCloseHandle() {
      if (this.receiptsclickRow) {
        this.receiptsclickRow = "";
        this.receiptsclickRowClone = "";
      }
    },
    dialogconfirmHandle() {},
    showValueHandle(val, row, idKey) {
      this.$set(row, idKey + "ShowValue", val);
    },
    sumsChangeHandle(arr) {
      if (arr[3] == arr[4] && arr[3]) {
        this.receipSum = ToString(arr[3]);
      } else {
        this.receipSum = "";
      }
    },

    receipPageChange() {},
    tableClick(row) {
      if (row != this.receiptsclickRow) {
        this.receiptsclickRow = {};
      }
      this.$nextTick(() => {
        this.receiptsclickRow = row;
        this.receiptsclickRowClone = deepClone(row);
      });
    },
    dialogconfirmHandle() {},
    onSelectionChange(val) {
      this.checkTable = val;
    },
    currentChangeHandle(pageModel) {
      this.pageModel = pageModel;
      this.$nextTick(() => {
        this.initMainTableHandele(this.searchForm);
      });
    },
    onSubmitHandle() {},
    searchSelectChangeHandle(val, index, indexRow, row) {},
    searchHandle() {
      this.initMainTableHandele(this.searchForm);
    },
    resizeSearchHandle() {
      this.searchForm = {};
    },
  },
  computed: {
    receiptsTitle() {
      return this.receiptsOptions.find((item) => item.id == this.receiptsSelect)
        .title;
    },
    subjectComputed() {},
  },
  created() {
    this.initMainTableHandele();
  },
};
</script>

<style scoped lang="scss">
.await {
  width: 100%;
  .search {
    padding: 20px;
    background-color: #fff;
    border-radius: 20px;
    .btn {
      display: flex;
      justify-content: flex-end;
    }
  }
  .maintable {
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    width: 100%;
    .left {
      background-color: #fff;
      padding: 20px;
      border-radius: 20px;
      width: 100%;
      > .footer {
        margin-top: 20px;
      }
    }
    .right {
      margin-left: 20px;
      background-color: #fff;
      border-radius: 20px;
      width: 25%;
      position: relative;
      > .wrap {
        padding: 20px;
        .title {
          font-size: 20px;
          margin-bottom: 20px;
        }
        > .footer {
          position: absolute;
          bottom: 20px;
          right: 10px;
        }
      }
    }
  }
}
.dialog {
  .receipts {
    text-align: center;
    position: relative;
    .img {
      position: absolute;
      top: 5%;
      left: 60%;
      z-index: 2;
    }
    .title {
      font-size: 20px;
      font-weight: 600;
    }
    .date {
      display: flex;
      justify-content: flex-end;
      align-items: center;
      .demonstration {
        margin-right: 30px;
      }
      .datepick {
        width: 200px;
      }
    }
    .main {
      margin-top: 10px;
      border: 1px solid #000;

      > .top {
        padding: 10px 10px 0;
        display: flex;
        justify-content: space-between;
        .left {
          display: flex;
          text-align: left;
          .select {
            width: 130px;
          }
          .input {
            margin-left: 10px;
            width: 100px;
          }
        }
        .right {
        }
      }
      > .table {
        padding: 0 10px 0;
        margin-top: 10px;
      }
      > .footer {
        border-top: 1px solid #000;
        margin-top: 10px;
        padding: 10px;
        display: flex;
        align-items: center;
        font-weight: 700;
        > .left {
          display: flex;
          flex-direction: column;
          align-items: flex-start;
          > .top {
            display: flex;
            justify-content: flex-start;
            align-items: center;
            .left {
              margin-right: 20px;
            }
          }
          > .bottom {
            margin-top: 10px;
            display: flex;
            justify-content: flex-start;
          }
        }
        > .right {
          padding: 20px;
          text-align: left;
          border: 1px solid #ccc;
          width: 80%;
          margin-left: 20px;
          height: 80px;
          display: flex;
          .chunk {
            width: 20%;
            display: flex;
            align-items: center;
            > .value {
              width: 60%;
              text-align: left;
              display: -webkit-box; /*作为弹性伸缩盒子模型显示。*/
              -webkit-box-orient: vertical; /*作为弹性伸缩盒子模型显示。*/
              -webkit-line-clamp: 1; //*显示的行*/
              overflow: hidden; /*溢出隐藏*/
              /* 对于连字情况或者纯字母，可以解决 */
              word-break: break-all;
            }
          }
        }
      }
    }
    footer {
      padding: 20px;
      margin-top: 20px;
      background-color: #e1e3e8;
      height: 80px;
      font-weight: 800;
      > .top {
        text-align-last: left;
        > .chunk {
          display: flex;
          font-weight: 800;
        }
      }
      > .bottom {
        margin-top: 10px;
        text-align-last: left;
        display: flex;
        justify-content: space-between;
        .chunk {
          margin-right: 20px;
          display: flex;
          font-weight: 800;
        }
      }
    }
  }
}
</style>
